package com.education.teach.mvp.ui.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.education.teach.R;
import com.education.teach.mvp.model.bean.PaperBean;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class ExamDetailAdapter extends BaseQuickAdapter<PaperBean.ResultBean.PaperTypeBean.PapertypeQuestionBean.QuestionBean, BaseViewHolder> {
    private Integer index = -1;
    private List<PaperBean.ResultBean.PaperTypeBean.PapertypeQuestionBean.QuestionBean> answerData = new ArrayList<PaperBean.ResultBean.PaperTypeBean.PapertypeQuestionBean.QuestionBean>();
    public ExamDetailAdapter(int layoutResId, @Nullable List<PaperBean.ResultBean.PaperTypeBean.PapertypeQuestionBean.QuestionBean> data) {
        super(layoutResId, data);
        answerData = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, PaperBean.ResultBean.PaperTypeBean.PapertypeQuestionBean.QuestionBean item) {
        WebView mWebView = helper.getView(R.id.wv_title);
        WebSettings webSettingsTitle = mWebView.getSettings();
        webSettingsTitle.setDisplayZoomControls(false); //隐藏webview缩放按钮
        webSettingsTitle.setJavaScriptEnabled(true);//支持js
        webSettingsTitle.setBuiltInZoomControls(true); // 显示放大缩小
        webSettingsTitle.setSupportZoom(true); // 可以缩放
//        String htmlTtile = "<p><img src=\"http://36.48.167.135:8084/study/ueditor1_3_6/jsp/upload/image/20180913/1536802367916025370.png\" title=\"1536802388695028246.png\" alt=\"image.png\"/></p>";
        mWebView.loadDataWithBaseURL(null, getNewContent(item.getTitle()), "text/html", "UTF-8", null);

        if (item.getUserAnswer() != null ){
            if (item.getUserAnswer().size() >0){
                helper.setText(R.id.et_answer, Html.fromHtml(item.getUserAnswer().get(0).getAnswer()));
            }
        }

        EditText et_answer = helper.getView(R.id.et_answer);
        et_answer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = (Integer) v.getTag();
                }
                return false;
            }
        });
        class MyTextWatcher implements TextWatcher {
            public MyTextWatcher(RecyclerView.ViewHolder holder) {
                mHolder = holder;
            }

            private RecyclerView.ViewHolder mHolder;

            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && !"".equals(s.toString())) {
                    int position = helper.getAdapterPosition();
                    answerData.get(position).setAnswerQes(s.toString());
//                    mData.get(position).put("list_item_inputvalue",
//                            s.toString());// 当EditText数据发生改变的时候存到data变量中
                }
            }
        }
        et_answer.addTextChangedListener(new MyTextWatcher(helper));

    }

    public static String getNewContent(String htmltext) {
        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }
        return doc.toString();
    }

    public List<PaperBean.ResultBean.PaperTypeBean.PapertypeQuestionBean.QuestionBean> getAnswer(){

        return answerData;
    }
}
